package com.qiaotongtianxia.loans.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.RadioGroup;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.adapters.LoansRecordAdapter;
import com.qiaotongtianxia.loans.bean.Refund;
import com.qiaotongtianxia.loans.functionInterface.IBaseRequestImp;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.CommonUtils;
import com.qiaotongtianxia.loans.util.ToastUtil;
import com.qiaotongtianxia.loans.views.BaseTextView;
import com.qiaotongtianxia.loans.views.DragLayout;
import com.qiaotongtianxia.loans.views.refrushRecyclerView.Action;
import com.qiaotongtianxia.loans.views.refrushRecyclerView.RefreshRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lisen on 2017/12/18.
 *贷款记录
 * @author lisen < 4533548588@qq.com >
 */

public class LoansRecordActivity extends BaseActivity implements DragLayout.DragListener{
    @BindView(R.id.tv_nav_title)
    BaseTextView tvNavTitle;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.refresh_view)
    RefreshRecyclerView refreshView;
    @BindView(R.id.dragLayout)
    DragLayout dragLayout;

    private LoansRecordAdapter adapter;
    private int type = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_loans_record;
    }

    @Override
    public Api onApiCreate() {
        return new Api(this);
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {

        dragLayout.setListener(this);

        initLayout();
        initEvent();
        requestDatas();

    }

    private void requestDatas() {
        api.loansRecord(type, new IBaseRequestImp<List<Refund>>() {
            @Override
            public void onRequestSuccess(List<Refund> refunds) {
                if (refunds.isEmpty()){
                    ToastUtil.showShort(LoansRecordActivity.this,"暂无记录");
                }
                adapter.clear();
                adapter.addAll(refunds);
                refreshView.dismissSwipeRefresh();
            }
            @Override
            public void onRequestError(int errorCode, String errorMsg) {
                refreshView.dismissSwipeRefresh();
            }
        });
    }

    private void initEvent() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (CommonUtils.isFastDoubleClick()) {
                    return;
                }
                adapter.clear();
                switch (checkedId) {
                    case R.id.rbtn_all:
                        type = 0;
                        break;
                    case R.id.rbtn_waitCheck:
                        type = 1;
                        break;
                    case R.id.rbtn_rejected:
                        type = 2;
                        break;
                    case R.id.rbtn_notRefund:
                        type = 3;
                        break;
                    case R.id.rbtn_refunding:
                        type = 4;
                        break;
                    case R.id.rbtn_refunded:
                        type = 5;
                        break;
                }
                requestDatas();
                refreshView.getRecyclerView().scrollToPosition(0);
            }
        });
    }

    private void initLayout() {
        refreshView.setLayoutManager(new LinearLayoutManager(this));
        refreshView.setLoadMoreAble(false);
        refreshView.getRecyclerView().setItemAnimator(null);//此处很重要，处理recyclerview快速切换崩溃
        adapter = new LoansRecordAdapter(this);
        refreshView.setAdapter(adapter);

        refreshView.setRefreshAction(new Action() {
            @Override
            public void onAction() {
                adapter.clear();
                refreshView.dismissSwipeRefresh();
                requestDatas();
            }
        });
    }

    @OnClick(R.id.iv_nav_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void initTitle() {
        tvNavTitle.setText("借款记录");
    }

    @Override
    public boolean isCanPullDown() {
        return false;
    }

    @Override
    public boolean isCanPullUp() {
        return CommonUtils.isReachBottom(refreshView.getRecyclerView());
    }
}
